package com.cade.agriculturemall.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Lobby on 2017/11/17.
 */

public abstract class FWFragment<P extends IBasePresenter> extends Fragment implements IBaseView {
    protected P mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = setupPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(setupLayout(), container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupView();
        setupEvent();
        setupData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.onResume();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
            mPresenter = null;
        }
    }

    public abstract int setupLayout();

    public abstract void setupView();

    public abstract void setupEvent();

    public abstract void setupData();

    public abstract P setupPresenter();

    @Override
    public void showProgress() {
        getBaseActivity().showProgress();
    }

    @Override
    public void showProgress(String msg) {
        getBaseActivity().showProgress(msg);
    }

    @Override
    public void showProgress(String msg, int progress) {
        getBaseActivity().showProgress(msg, progress);
    }

    @Override
    public void dismissProgress() {
        getBaseActivity().dismissProgress();
    }

    @Override
    public void showError(String msg) {
        getBaseActivity().showError(msg);
    }

    @Override
    public void showSuccess(String msg) {
        getBaseActivity().showSuccess(msg);
    }

    @Override
    public void showMessage(String msg) {
        getBaseActivity().showMessage(msg);
    }

    public FWActivity getBaseActivity() {
        return (FWActivity) getActivity();
    }

    @Override
    public void goBack() {

    }
}
